﻿/**
 * 跨平台 Bridge (_bridge)
 * 支持 iOS WKWebView & Android WebView
 */

const config = {
    configUrl: 'https://twines-web.smartapp.net.cn/',
    baseApi: 'https://twines-web.smartapp.net.cn/WebApi',
    serverPath: '',
    imageUrl: 'https://twines-web.smartapp.net.cn/ConfigApi/api/file/download/image?fileid=',
    avatarUrl: 'https://twines-web.smartapp.net.cn/ConfigApi/api/file/download/avatar?fileid=',
    imDomainUrl: 'https://im-fileapi.smartapp.net.cn/rest/v3/im/file/download?fileId=',
    languageType: 0,
};
const userInfo = {
    token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzaSI6IjEwMzQ1NTc1IiwidWkiOiIzNCIsImV1aSI6IjY5MTViMDZkMzgxZmEwZWU4YTMzNTA1OCIsImdkIjoiMGMzMjUzZjEtZTAxYi00MmQ0LTlkOWEtZDZkYTk3ZDU5ZDhjIiwibHMiOiIxIiwibmJmIjoxNzYzMTAxODAzLCJleHAiOjE3NjU2OTM4MDMsImlzcyI6IlN0ZWFkeS5BcHAiLCJhdWQiOiJTdGVhZHkuQXBwIn0.lTl53ZHPhCNC1xorjnq4Pr7KR7VTarZLRVuei4XBF60',
};

window._bridge = {
    _callbacks: {},
    callNative(method: string, params: { [key: string]: any } = {}) {
        return new Promise(resolve => {
            const callbackId = 'cb_' + Date.now() + '_' + Math.random().toString(36).substring(2);
            this._callbacks[callbackId] = resolve;

            const message = { method, params, callbackId };

            // iOS WKWebView
            if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.iosBridge) {
                window.webkit.messageHandlers.iosBridge.postMessage(message);
            }
            // Android WebView
            else if (window.AndroidBridge && window.AndroidBridge.postMessage) {
                window.AndroidBridge.postMessage(JSON.stringify(message));
            } else {
                if (method === 'getAppConfig') {
                    resolve(config);
                } else if (method === 'getAppUserInfo') {
                    resolve(userInfo);
                } else {
                    console.warn('NativeBridge not available');
                }
            }
        });
    },
    _resolve(callbackId: string, result: any) {
        if (this._callbacks[callbackId]) {
            this._callbacks[callbackId](result);
            delete this._callbacks[callbackId];
        }
    },
};

/**
 * _bridge.callNative(method: string, params = {}) 调用原生api的方法 异步
 *
 *
 *  @method getAppConfig 获取app配置信息
 *
 *  @method getAppUserInfo 获取app用户信息
 *
 *  @method setTitle 设置标题
 *    @param {name: string, subName: string}
 *
 *  @method setNavRightBtn 设置导航栏右侧按钮
 *    @param {type: boolean, name: string}
 *    - type 1 (文字按钮)|  2 (图标按钮) | 3 (下拉按钮)
 *    - name 按钮名称获图标名
 *
 *  @method goBack  返回
 *
 *  @method setGroupNoticeButtonStatus 设置群公告按钮状态
 *    @param {key: string, status: boolean}
 *    - key: bold（加粗） | ol（无序列表） | ul （有序列表），
 *    - status: true | false
 *
 *  @method hideNavRightBtn 隐藏导航栏右侧按钮
 *
 *  @method callAppRefreshGroupNoticeList 刷新群公告列表
 *    @param {groupAnnouncementId: string}
 *
 *  @method h5DownloadImage 下载图片
 *     @param {dataList: string[], index: number, tenantId: string}
 *
 *  @method h5DownloadFile 下载文件
 *     @param {fileName: string, filePath: string, fileSize: number, tenantId: string}
 *
 *
 *
 */
